package com.office.supplies.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.office.supplies.mapper.BuyMapper;
import com.office.supplies.mapper.GoodsMapper;
import com.office.supplies.mapper.SaleMapper;
import com.office.supplies.mapper.StockMapper;
import com.office.supplies.pojo.Buy;
import com.office.supplies.pojo.Goods;
import com.office.supplies.pojo.Sale;
import com.office.supplies.pojo.Stock;
import com.office.supplies.pojo.dto.AddGoodsDTO;
import com.office.supplies.service.GoodsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * @Author admin
 * @Description TODO
 * @Date 2023/12/11/0:56
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private BuyMapper buyMapper;

    @Autowired
    private SaleMapper saleMapper;

    @Autowired
    private StockMapper stockMapper;

    //处理地址
    private static Goods getGoodsAndGoodsAddress(AddGoodsDTO addGoodsDTO) {
        Goods goods = new Goods();
        BeanUtils.copyProperties(addGoodsDTO, goods);

        //处理地址
        String[] goodAddresses = addGoodsDTO.getGoodsAddress();
        // 创建一个 StringBuilder 对象
        StringBuilder goodAddress = new StringBuilder();

        // 循环遍历数组并将内容添加到 StringBuilder
        for (String str : goodAddresses) {
            goodAddress.append(str);
        }
        goods.setGoodsAddress(goodAddress.toString());
        return goods;
    }

    /**
     * 新增商品
     *
     * @param addGoodsDTO AddGoodsDTO
     */
    public void insertGoods(AddGoodsDTO addGoodsDTO) {
        Goods goods = getGoodsAndGoodsAddress(addGoodsDTO);
        // 生成唯一标识
        // 生成UUID作为商品编号的一部分
        UUID uuid = UUID.randomUUID();
        // 将UUID转换为字符串作为商品编号
        String goodsCode = "GOODS-" + uuid.toString();
        goods.setGoodsCode(goodsCode);

        goodsMapper.insertGoods(goods);
    }

    /**
     * TODO 根据id删除商品
     *
     * @param id Long
     */
    @Transactional
    public void deleteGoodsById(Long id) {
//        // 同时查询该goodsId是否存在 采购，库存，销售信息
//
//        // 1.采购
//        int i = buyMapper.selectBuyByGoodsId(id);
//
//        //2.库存
//        int i1 = stockMapper.selectStockByGoodsId(id);
//
//        //3.销售
//        int i2 = saleMapper.selectSaleByGoodsId(id);
//
//        if( i + i1 + i2 > 0){
//            throw new RuntimeException("请先删除该商品对应的采购/库存/销售/信息！");
//        }

        // 删除
        goodsMapper.deleteGoodsById(id);

        // 1.删除采购
        buyMapper.delete(
                new LambdaQueryWrapper<Buy>()
                        .eq(Buy::getGoodsId, id)
        );

        // 2.删除库存
        stockMapper.delete(
                new LambdaQueryWrapper<Stock>()
                        .eq(Stock::getGoodsId, id)
        );

        // 3.删除销售
        saleMapper.delete(
                new LambdaQueryWrapper<Sale>()
                        .eq(Sale::getGoodsId, id)
        );
    }

    /**
     * 修改商品
     *
     * @param goods Goods
     */
    public void updateGoods(Goods goods) {
        goodsMapper.updateGoods(goods);
    }

    /**
     * 条件分页查询商品
     *
     * @param pageNum Integer
     * @param pageSize Integer
     * @param addGoodsDTO AddGoodsDTO
     * @return PageInfo<Goods>
     */
    public PageInfo<Goods> getAllGoodsByPageAndCondtion(Integer pageNum, Integer pageSize, AddGoodsDTO addGoodsDTO) {
        PageHelper.startPage(pageNum,pageSize);
        Goods goods = getGoodsAndGoodsAddress(addGoodsDTO);
        List<Goods> goodsList = goodsMapper.getGoodsList(goods);
        return new PageInfo<>(goodsList);
    }

    /**
     * 查询所有商品
     *
     * @return List<Goods>
     */
    public List<Goods> getGoodsList() {
        return goodsMapper.getGoodsList(null);
    }


}
